// poi-details.js
import { http } from '../../server/http.server'
import { httpRouter } from '../../server/http.router'
import { _ } from '../../libs/underscore/underscore.modified'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    spotpois:{},
    tabIndex:0,
    voiceSrc:"",
    playStatus:"",
    loadStatus:true
  },
  onShareAppMessage: function (res) {
    return {
      title: 'Ai导游',
      path: '/pages/index/index',
      success: function (res) { },
      fail: function (res) { }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //let poid = "20150814000001";
    this.getPoi(options.poiId);
    //this.getPoi(poid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.audioCtx = wx.createAudioContext('audio');
  },
  switchTab:function(e){
    this.setData({
      tabIndex:e.currentTarget.id
    })
  },
  voiceControl: function (e) {
    let that = this;
    if (this.data.playStatus != e.currentTarget.id) {
      this.setVoiceSrc(e.currentTarget.id, function () {
        that.audioCtx.play();
        that.setData({
          playStatus: e.currentTarget.id
        });
      });

    } else {
      this.audioCtx.pause();
      this.setData({
        playStatus: ""
      });
    }

  },
  voiceControl1:function(e){
    let that = this;
    if (this.data.playStatus != e.currentTarget.id){
      this.audioCtx.pause();
      this.setVoiceSrc(e.currentTarget.id,function(){
        that.audioCtx.play();
        that.setData({
          playStatus: e.currentTarget.id
        });
      });
      
    }else{
      this.audioCtx.pause();
      this.setData({
        playStatus: ""
      });
    }
    
  },
  setVoiceSrc: function (contentId,callback){
    let that = this;
    let PoiContent = _.find(this.data.spotpois.PoiContents, function (item) {
      if (item.ContentId == contentId) {
        return item;
      }
    });
    this.audioCtx.setSrc(encodeURI(PoiContent.ContentUri));
    callback()
  },
  voiceEnd:function(){
    this.setData({
      playStatus: ""
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getPoi: function (poiId){
    let that = this;
    let url = httpRouter.spotPoi(poiId);
    http.get(url).subscribe({
      next: res => {
        that.setData({
          spotpois: res.data.Content,
          loadStatus:false
        })
      },
      error: err => console.log(err)
    })
  },

  play:function(){
    this.audioCtx.play()
  }
})